/*
 * Author: xiguan
 * Mail: xiguan.teng@qq.com
 * -----
 * Last Modified: 2022-05-31 22:09:27
 * Modified By: xiguan
 * -----
 * Copyright (c) 2022 XiGuan Inc.
 * 
 * No Bug No error No warning.
 */



#include <bits/stdc++.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>


using namespace std;

int g_var = 5;

int main(int argc, char *argv[]){

    pid_t pid;
    int var = 1, i;
    
    cout << "fork is diffirent with vfork " << endl;

    // pid = fork();
    pid = vfork();

    switch(pid){
        case 0:{
            i = 3;
            while((i --) > 0){
                printf("Child process is running\n");
                g_var ++;
                var ++;
                sleep(1);
            }
            printf("Child's g_var = %d, var = %d\n", g_var, var);
            break;
        }
        case -1:{
            perror("fork or vfork");
            exit(0);
            break;
        }
        default:{
            i = 5;
            while((i --) > 0){
                printf("Parent process is running\n");
                g_var ++;
                var ++;
                sleep(1);
            }
            printf("Parent's g_var = %d, var = %d\n", g_var, var);
            // exit(0);
            break;
        }
    }
    

    exit(1);
}